package agents;

import constants.*;

import interfaces.ActionInterface;
import interfaces.impl.Agent;
import interfaces.impl.actions.ActionModifySpetialFeature;

import java.awt.Graphics;
import java.util.ArrayList;

import actions.ActionAttack;

import objects.SpetialFeature;
import objects.WorldObject;
import universe.Universe;

public class AgentBombermanCastle extends Agent{
	
	private ActionModifySpetialFeature modifyX;
	private ActionModifySpetialFeature modifyY;
	private ActionAttack actionAttack;
	
	private double delayTime=0;
	
	public AgentBombermanCastle(){
		this.id=Constants.AGENT_BOMBERMAN_CASTLE;
		
		listActions= new ArrayList<ActionInterface>();
		
		modifyX=new ActionModifySpetialFeature();
		listActions.add(modifyX);
		
		modifyY= new ActionModifySpetialFeature();
		
		actionAttack= new ActionAttack();
		listActions.add(actionAttack);
		
		
	}

	@Override
	public void update(WorldObject object, Universe worldI, double time) {
		if(!canUpdate(object)){
			System.out.println("Me falta alguna accion para poder actuar");
			return;
		}
		double damage=0;
		double currentLife=0;
		double attackTime=0;
		
		int cont=0;
		for(SpetialFeature fe : object.getListFeatures()){
			if(fe.getId()==Constants.SPETIAL_FEATURE_DAMAGE){
				damage=fe.getValue();
				cont++;
			}
			if(fe.getId()==Constants.SPETIAL_FEATURE_CURRENT_LIFE){
				currentLife=fe.getValue();
				cont++;
			}
			if(fe.getId()==Constants.SPETIAL_FEATURE_ATTACK_TIME){
				attackTime=fe.getValue();
				cont++;
			}
			
		}
		if(cont!=3){
			System.out.println("Me faltan spetial features Agent Bomebarmn");
			return;
		}
		
		
		if(currentLife<=0){
			worldI.getListObjectsToKill().add(object);
		}
		
		WorldObject targetCastle = null;
		WorldObject targetMonster = null;
		
		for(WorldObject obj : worldI.getListObjects()){
			if(obj.getObjectReference()==Constants.OBJECT_REFERENCE_CASTLE_RED){
				targetCastle=obj;
				//System.err.println("dist "+Constants.CalculateDistance(obj.getPosition(), object.getPosition()));
				//System.err.println("dist "+object.getImageObject().getImageToShow().getWidth());
				
				if(factory.Constants.CalculateDistance(obj.getPosition(), object.getPosition())<=object.getImageObject().getImageToShow().getWidth()){
					System.out.println("Asigno Target a Atacar");
					targetMonster=obj;
				}
				
			}
			if(obj.getObjectReference()==Constants.OBJECT_REFERENCE_FUNGI){
				if(factory.Constants.CalculateDistance(obj.getPosition(), object.getPosition())<=object.getImageObject().getImageToShow().getWidth()){
					if(targetMonster==null){
						targetMonster=obj;
					}
				}
			}
		}
		
		if(targetCastle==null){
			return;
		}
		
		delayTime=delayTime+time;
		if(delayTime>=attackTime){
			delayTime=attackTime;
		}
		
		double x,y;
		if(targetMonster==null){
			x=targetCastle.getPosition().getX();
			y=targetCastle.getPosition().getY();
		}
		else{
			x=object.getPosition().getX();
			y=object.getPosition().getY();
			if(delayTime==attackTime){
				//System.out.println("Estoy atacando");
				actionAttack.setSource(object);
				actionAttack.setTarget(targetMonster);
				object.setImmediatlyActionToDo(actionAttack);
				delayTime=0;
			}
			
		}
		
		modifyX.setSource(object);
		modifyX.setTarget(object);
		modifyX.setFeatureId(Constants.SPETIAL_FEATURE_X_TO_MOVE);
		modifyX.setValue(x);
		object.setImmediatlyActionToDo(modifyX);
		
		modifyY.setSource(object);
		modifyY.setTarget(object);
		modifyY.setFeatureId(Constants.SPETIAL_FEATURE_Y_TO_MOVE);
		modifyY.setValue(y);
		object.setImmediatlyActionToDo(modifyY);
		
	}

	@Override
	public void paint(Graphics g, WorldObject object) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "Bomberman castles";
	}

	@Override
	public void setName(String name) {
		// TODO Auto-generated method stub
		
	}
	

}
